import { Loading, Message } from "element-ui";
let loading,
    timer,
    needLoadingRequestCount = 0;

function startLoading() {
    loading = Loading.service({
        lock: true,
        text: "Loading...",
        spinner: "el-icon-loading",
        background: "rgba(255,255,255,.5)",
    });
}

//打开loading
export function showFullScreenLoading(time = 300) {
    if (needLoadingRequestCount === 0) {
        if (timer) clearTimeout(timer);
        timer = setTimeout(() => {
            startLoading();
        }, time);
    }
    needLoadingRequestCount++;
}

//关闭loading
export function tryHideFullScreenLoading() {
    if (needLoadingRequestCount <= 0) return;
    needLoadingRequestCount--;
    if (needLoadingRequestCount === 0) {
        if (timer) clearTimeout(timer);
        setTimeout(() => {
            if (loading) loading.close();
        }, 300);
    }
}
